import BpmnModeler from "bpmn-js/lib/Modeler";

import customPalette from '@/components/Workflow/CustomDefaultProperties/CustomPalette'
import customContextPadProvider from '@/components/Workflow/CustomDefaultProperties/ContextPadProvider'
import { i18nTranslate } from '@/i18n/bpmn'
import Utils from "@/utils/utils";

/**
 * 创建流程图模型
 * @param canvas
 * @param extensions 用于扩展对应三方xml属性的配置
 * @returns {Modeler}
 */
let createBpmnModeler = (canvas,extensions)=>{
    let customTranslateModule = {
        translate: [ 'value', i18nTranslate ]
    };
    let modelOptions = {
        container: canvas,
        additionalModules:[
            customTranslateModule,
            customPalette,
            customContextPadProvider
        ]
    }
    if(extensions){
        modelOptions.moddleExtensions = extensions
    }
    return new BpmnModeler(modelOptions);
}

let initialDiagram =
    '<?xml version="1.0" encoding="UTF-8"?>' +
    '<bpmn:definitions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
    'xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" ' +
    'xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" ' +
    'xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" ' +
    'targetNamespace="http://bpmn.io/schema/bpmn" ' +
    'id="{definitionId}">' +
    '<bpmn:process id="{processId}" isExecutable="false" >' +
    '</bpmn:process>' +
    '<bpmndi:BPMNDiagram id="BPMNDiagram_1">' +
    '<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="{processId}">' +
    '</bpmndi:BPMNPlane>' +
    '</bpmndi:BPMNDiagram>' +
    '</bpmn:definitions>';

let getDefaultBpmXmlContent = (processId= null,definitionId = null) => {
    let result = initialDiagram;
    if(processId == null){
        let generateRandomString = Utils.generateRandomString("Process_");
        result = result.replace(/\{processId\}/g,generateRandomString)
    }
    if(definitionId == null){
        let generateRandomString = Utils.generateRandomString("Definition_");
        result = result.replace(/\{definitionId\}/g,generateRandomString)
    }
    console.log(result)
    return result
}


export default {
    createBpmnModeler,
    getDefaultBpmXmlContent
};